<?php 
ini_set("memory_limit", "32M");
if(!isset($_SESSION)) 
{ 
session_start(); 
} 
include 'checkdate.php';

 
if (isset($_REQUEST['m_fromdate']) and isset($_REQUEST['m_todate']) and isset($_REQUEST['carnumber'])) {
	unset($_SESSION['s_trace']);
	unset($_SESSION['trace_order']);
	unset($_SESSION['carnumbertrace']);
	include '../connection/connection.php';
	set_time_limit(0);
	$m_fromdate = $_REQUEST['m_fromdate']; 
	$m_todate = $_REQUEST['m_todate'];
	$carnumber = $_REQUEST['carnumber'];
	$winH = 490;
	if(isset($_REQUEST['winH'])) $winH = $_REQUEST['winH'];
	$_SESSION['m_fromdate'] = $m_fromdate; 
	$_SESSION['m_todate'] = $m_todate;
	$_SESSION['carnumbertrace'] = $carnumber;
	$page = 1;
	$rows_per_page = 100;
	$number_of_page = 0;
	$start_p = 0; $end_p = $rows_per_page*$page;
	//echo "SELECT cn.id,cof.name,cn.carnumber,cn.car_datetime,cof.pos_x,cof.pos_y,count(cn.carnumber) as solan FROM carnum cn,configcamera cof WHERE cn.id=cof.id and cn.car_datetime > '".$m_fromdate."' and cn.car_datetime < '".$m_todate."' and cn.carnumber like '%".$carnumber."%' GROUP BY cn.carnumber order by cn.car_datetime";
	$listSearchs = mysql_query("SELECT count(*) FROM carnum cn,configcamera cof WHERE cn.id=cof.id and cn.car_datetime > '".$m_fromdate."' and cn.car_datetime < '".$m_todate."' and cn.carnumber like '%".$carnumber."%' GROUP BY cn.carnumber order by cn.car_datetime",$connection);
	$listSearch = mysql_query("SELECT cn.id,cof.name,cn.carnumber,cn.car_datetime,cof.pos_x,cof.pos_y,count(cn.carnumber) as solan FROM carnum cn,configcamera cof WHERE cn.id=cof.id and cn.car_datetime > '".$m_fromdate."' and cn.car_datetime < '".$m_todate."' and cn.carnumber like '%".$carnumber."%' GROUP BY cn.carnumber order by cn.car_datetime limit 0,100",$connection);
	
	$index = 0;
	
	?>
	<div id="divscroll" style="overflow:scroll;overflow-x: hidden; width:100%; height:<?=$winH?>px; background-color: #ffffff">
	               <table cellpadding="0" cellspacing="0" border="0" width="100%" class="tbchung" style="margin-top:0" id="tablebody">
						 <tbody>
						 
	<?php 

	if (mysql_num_rows( $listSearchs )>0) {
		$number_of_page = ceil ( mysql_num_rows( $listSearchs ) / $rows_per_page );
	}
	
	while ($row = mysql_fetch_array($listSearch)){
		$index++;
		if ( $i <=$rows_per_page ){
			?>
					 <tr>
					 	<td width="25%"><?=$row['id']?></td>
					 	<td width="25%"><?=$row['name']?></td>
					 	<td width="25%"><?=$row['carnumber']?></td>
					 	<td width="25%"  class="last"><?=$row['solan']?></td>
					 </tr> 
					 <?php 
		}
		
		
	 }
	 if ($index>0 and $index<$winH/28) {
	 	foreach(range(0,$winH/28-$index) as $i){
	 		?>
	 		<tr>
		 	<td width="25%">&nbsp;</td>
		 	<td width="25%">&nbsp;</td>
		 	<td width="25%">&nbsp;</td>
		 	<td width="25%"  class="last">&nbsp;</td>
		 	</tr>
	 	<?php }
	}
	
	 ?>
	</tbody>       
		               </table>
	</div>
	<?php
	if ($number_of_page >1){?>
			<table cellpadding="0" cellspacing="0" border="0" width="100%" class="tbchung" id="tablefooter">
			<tr>
						<td class="rf-dt-ftr-c" style="text-align: center; padding:1px 0px;border:0 !important" >
								<span style="padding:1px; display: inline-block;">
															
									<?php if ( $number_of_page > 1 ) 
									{ 
										$numberperpage = 15;
										$thuong = intval($page/$numberperpage);
										if ($numberperpage>$number_of_page) {
											$pp = $number_of_page;
											$p = 1;
										}
										else if($page<$numberperpage){
											$pp = $numberperpage;
											$p = 1;
										}else{
											$sodu = intval($page%$numberperpage);
											if ($sodu==0) $thuong = $thuong-1;
											$p = ($thuong*$numberperpage)+1;
											$pp = $p+$numberperpage-1;
											if($pp>=$number_of_page){
												$pp = $number_of_page;
											}
										}
										
										if($p>$numberperpage){?>
											<a onclick="OnPagging('<?=$m_fromdate?>','<?=$m_todate?>','<?=$carnumber?>',<?=1?>,<?=$rows_per_page?>,<?=$number_of_page?>)"><span class='button_next'><?php echo "<<<";?></span> </a>
											<a onclick="OnPagging('<?=$m_fromdate?>','<?=$m_todate?>','<?=$carnumber?>',<?=($p-1)?>,<?=$rows_per_page?>,<?=$number_of_page?>)"><span class='button_next'><?php echo "<<";?></span> </a>
										<?php  }
										else{
											echo "<span class='button_next' style='cursor:auto'><<<</span> "; 
											echo "<span class='button_next' style='cursor:auto'><<</span> ";
										}
										for ( $i = $p; $i <= $pp; $i++ ) {	
												if ($i<=$number_of_page && $i>0){
													if ($i==$page){
														echo "<span class='button_next' style='color: blue;'><b>".$i."</b></span>"; 
													}else{
													?>
														<a onclick="OnPagging('<?=$m_fromdate?>','<?=$m_todate?>','<?=$carnumber?>',<?=$i?>,<?=$rows_per_page?>,<?=$number_of_page?>)"><span class="button_next">
														<?php echo $i;?>
														</span> </a>
														<?php 
													}}else break;
												?>
											
										<?php 
												}
											
											if($pp<$number_of_page){ ?>
												<a onclick="OnPagging('<?=$m_fromdate?>','<?=$m_todate?>','<?=$carnumber?>',<?=$pp+1?>,<?=$rows_per_page?>,<?=$number_of_page?>)"><span class='button_next'><?php echo ">>";?></span> </a>
												<a onclick="OnPagging('<?=$m_fromdate?>','<?=$m_todate?>','<?=$carnumber?>',<?=$number_of_page?>,<?=$rows_per_page?>,<?=$number_of_page?>)"><span class='button_next'><?php echo ">>>";?></span> </a>
											<?php }
											else{
												echo "<span class='button_next' style='cursor:auto'>>></span> ";
												echo "<span class='button_next' style='cursor:auto'>>>></span> ";
											}
										}
										
										?>
								</span>
	               			 </td>
						</tr> 
					</table>
					  
			<?php }
	  
	
	 mysql_close($connection);
	  
}
?>

